package com.wangzhenyu.learn.test.android_.fragment_

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import com.wangzhenyu.common.base.BaseVBFragment
import com.wangzhenyu.common.util.log.LogUtil
import com.wangzhenyu.common.util.permission.PermissionUtil
import com.wangzhenyu.common.util.storage.StorageUtil
import com.wangzhenyu.learn.R
import com.wangzhenyu.learn.databinding.TestFragmentThirdBinding
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream

class DocumentFragment : BaseVBFragment<TestFragmentThirdBinding>() {


    override fun getBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ) = TestFragmentThirdBinding.inflate(inflater, container, false)


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        //调用摄像头拍照
        val register = registerForActivityResult(ActivityResultContracts.TakePicturePreview())
        {
            binding?.image?.setImageBitmap(it)
        }


        //申请存储权限
        val registerPermission =
            registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions())
            {
                for (i in it) {
                    if (i.value) {
                        LogUtil.default01("${i.key} 成功申请了")
                    }
                }
            }


        binding?.apply {

            //调用摄像头拍照
            bt01.setOnClickListener {
                register.launch(null)
            }

            //申请存储权限
            bt02.setOnClickListener {
                PermissionUtil.launchPermissions(registerPermission)
            }

            //图片保存在外部存储空间的应用专属空间中
            bt03.setOnClickListener {

                val temp = StorageUtil.savePicture(
                    requireContext(),
                    "sun.jpg",
                    BitmapFactory.decodeStream(requireContext().resources.openRawResource(R.raw.sun03))
                )

                if (temp) {
                    LogUtil.success()
                }
            }

            bt04.setOnClickListener {
//0:45

                //       val fileName = "myFile"
                //        val appSpecificExternalDir = File(context.getExternalFilesDir(null), fileName)
                //        if (!appSpecificExternalDir.exists()) appSpecificExternalDir.createNewFile()
                //        LogUtil.assess {
                //            appSpecificExternalDir.exists()
                //        }
                //        LogUtil.default01(appSpecificExternalDir.absolutePath)

                var openOutputStream: OutputStream? = null
                val file = File(requireContext().getExternalFilesDir(null), "sun.jpg")
                LogUtil.default01(file.absolutePath)
                try {
                    openOutputStream = FileOutputStream(file)
                    BitmapFactory.decodeStream(requireContext().resources.openRawResource(R.raw.sun05))
                        .compress(Bitmap.CompressFormat.JPEG, 100, openOutputStream)

                } catch (e: Exception) {
                    LogUtil.fail()
                } finally {

                    openOutputStream?.close()
                }


            }

        }
    }


}